package com.newcapec.modules.health.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newcapec.common.utils.R;
import com.newcapec.modules.health.entity.EducateDeptEntity;
import com.newcapec.modules.health.service.EducateDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 组织机构表
 *
 * @author fujun
 * @email fujun@newcapec.com
 * @date 2021-12-09 14:24:21
 */
@RestController
@RequestMapping("health/educatedept")
public class EducateDeptController {

    @Autowired
    private EducateDeptService educateDeptService;

    /**
     * 查询所有
     */
    @GetMapping("/list")
    @RequiresPermissions("health:educatedept:list")
    public R list(){
        List<EducateDeptEntity> list = educateDeptService.list();

        return R.ok().put("list", list);
    }

    /**
     * 列表全部
     */
    @GetMapping("/listAll")
    @RequiresPermissions("health:educatedept:list")
    public R listAll(@RequestParam Map<String, Object> params){
        Object customerId = params.get("customerId");
        QueryWrapper<EducateDeptEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(customerId!=null, "customer_id", customerId);
        List<EducateDeptEntity> list = educateDeptService.list(queryWrapper);

        return R.ok().put("list", list);
    }

    /**
     * 列表
     */
    @GetMapping("/find")
    @RequiresPermissions("health:educatedept:list")
    public R find(){
        List<EducateDeptEntity> list = educateDeptService.find();

        return R.ok().put("list", list);
    }

    /**
     * 根据学校id查询所有的院系
     */
    @GetMapping("/find/{customerId}")
    @RequiresPermissions("health:educatedept:list")
    public R findyuanxi(@PathVariable("customerId") Long customerId){
        QueryWrapper<EducateDeptEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(customerId!=null, "customer_id", customerId);
        queryWrapper.eq(customerId!=null, "level", 1);
        List<EducateDeptEntity> list = educateDeptService.list(queryWrapper);
        return R.ok().put("list", list);
    }


    /**
     * 根据院系id查询所有的专业
     */
    @GetMapping("/findmajors/{deptId}")
    @RequiresPermissions("health:educatedept:list")
    public R findmajors(@PathVariable("deptId") Long deptId){
        QueryWrapper<EducateDeptEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(deptId!=null, "parent_id", deptId);
        queryWrapper.eq(deptId!=null, "level", 2);
        List<EducateDeptEntity> list = educateDeptService.list(queryWrapper);
        return R.ok().put("list", list);
    }
    /**
     * 根据专业id查询所有的班级
     */
    @GetMapping("/findclass/{deptId}")
    @RequiresPermissions("health:educatedept:list")
    public R findclass(@PathVariable("deptId") Long deptId){
        QueryWrapper<EducateDeptEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(deptId!=null, "parent_id", deptId);
        queryWrapper.eq(deptId!=null, "level", 3);
        List<EducateDeptEntity> list = educateDeptService.list(queryWrapper);
        return R.ok().put("list", list);
    }
    /**
     * 信息
     */
    @GetMapping("/info/{deptId}")
    @RequiresPermissions("health:educatedept:info")
    public R info(@PathVariable("deptId") Long deptId){
		EducateDeptEntity educateDept = educateDeptService.getById(deptId);

        return R.ok().put("educateDept", educateDept);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("health:educatedept:save")
    public R save(@RequestBody EducateDeptEntity educateDept){
		educateDeptService.save(educateDept);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @RequiresPermissions("health:educatedept:update")
    public R update(@RequestBody EducateDeptEntity educateDept){
		educateDeptService.updateById(educateDept);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @RequiresPermissions("health:educatedept:delete")
    public R delete(Long[] ids){
		educateDeptService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
